
import Router from 'vue-router'

import HomeLayout from '@/layout/home-layout'
// import SingleLayout from '../layout/single-layout'

import store from '@/store'
import index_privs from "../router/_index_privs";

function findPath(list, path) {
  return list.some(it => {
    if (it.path) {
      return it.path == path
    } else if (it.children) {
      return findPath(it.children, path)
    }
    return false
  })
}
/* --------------------------------------------------------------------------------- */
/* -----------------------------  配置路由  ---------------------------------- */
/* --------------------------------------------------------------------------------- */


import page_list from './_page_load'
import _auto_page_generate from './_auto_page_generate'

const routes = [
  {
    path: '/',
    component: HomeLayout,
  },
  {
    path: "",
    component: HomeLayout
  },

  // 带菜单的配置页
  {
    path: '/page/*',
    name: 'HomeLayout',
    component: HomeLayout,
    children: [
      ...page_list,
      ..._auto_page_generate.map(it => {
        let { component, ...routeItem } = it
        return routeItem
      }),
      {
        path: '*',
        redirect: '/page/base/not_found',
      },
    ],
  },
  {
    path: '*',
    redirect: '/page/base/not_found',
  },
]

import Vue from 'vue'
Vue.use(Router)

const RouterInstance = new Router({
  base: '/',
  mode: 'history',
  routes: routes
})

import { Notification } from 'element-ui';
RouterInstance.beforeEach((to, from, next) => {

  if (store.state.stopRoute != true) {
    if (Vue.prototype.$loginUser.isSuperAdmin()) {
      store.commit("routerFrom", from)
      next()
      return
    } else if (findPath(index_privs, to.fullPath)) {
      if (Vue.prototype.$loginUser.menus.includes(to.fullPath)) {
        store.commit("routerFrom", from)
        next()
      } else {
        Notification.error({
          title: Vue.prototype.$tt('tip_missing_access_permission'),
          message: to.fullPath,
        });
        next(false)
      }
      return
    }
    store.commit("routerFrom", from)
    next()
  } else {
    next(false)
  }


})
RouterInstance.onError(e => {

})
export default RouterInstance


